package tomenttosa.prunuus.example.util;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

//import com.alibaba.fastjson.JSON;

public class FastjsonUtils {
	
	public void test() throws Exception{
		ProtectionDomain protectionDomain = this.getClass().getProtectionDomain();
		CodeSource codeSource = protectionDomain.getCodeSource();
		URI location = codeSource == null ? null : codeSource.getLocation().toURI();
	    String path = location == null ? null : location.getSchemeSpecificPart();
	    
	    if (path == null) {
	        throw new IllegalStateException("Unable to determine code source archive");
	      }
	      File root = new File(path);
	      if (!root.exists()) {
	        throw new IllegalStateException("Unable to determine code source archive from " + root);
	      }
	      System.out.println(path);
	      System.out.println(root.isDirectory() );
	    
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void test2() throws Exception{
		String dirBase = "C:/Users/jiashengxue/git/prunuus-example/prunuus-example-util/build/libs/";

		File file = new File(dirBase, "libs");
		String[] jars = file.list();
		URL[] jarUrls = new URL[jars.length];
		int i = 0;
		for (String jar : jars) {
		    File fileJar = new File(file, jar);
		    jarUrls[i++] = fileJar.toURI().toURL();
		    System.out.println(fileJar);
		}

		URLClassLoader classLoader = new URLClassLoader(jarUrls, FastjsonUtils.class.getClassLoader());

		Thread.currentThread().setContextClassLoader(classLoader);

		Class classToLoad = Class.forName("com.alibaba.fastjson.JSON", true, classLoader);
		
		Random rand = new Random();
		Map map = new HashMap();
		for(int j = 0; j< 10; j++){
			int rint = rand.nextInt(1000);
			map.put("k" + rint, "v" + rint);
		}

		Method method = classToLoad.getDeclaredMethod("toJSONString", Object.class);
		Object result = method.invoke(null, map);
		System.out.println(result);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void test3() throws Exception{
		String dirBase = "C:/Users/jiashengxue/git/prunuus-example/prunuus-example-util/build/libs/";

		File file = new File(dirBase, "libs");
		String[] jars = file.list();
		URL[] jarUrls = new URL[jars.length];
		int i = 0;
		for (String jar : jars) {
		    File fileJar = new File(file, jar);
		    jarUrls[i++] = fileJar.toURI().toURL();
		    System.out.println(fileJar);
		}

		URLClassLoader classLoader = new URLClassLoader(jarUrls, FastjsonUtils.class.getClassLoader());

		Thread.currentThread().setContextClassLoader(classLoader);

		Class classToLoad = Class.forName("com.alibaba.fastjson.JSON", true, classLoader);
		
		Random rand = new Random();
		Map map = new HashMap();
		for(int j = 0; j< 10; j++){
			int rint = rand.nextInt(1000);
			map.put("k" + rint, "v" + rint);
		}

		Method method = classToLoad.getDeclaredMethod("toJSONString", Object.class);
		Object result = method.invoke(null, map);
		System.out.println(result);
	}

//	@SuppressWarnings({ "rawtypes", "unchecked" })
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args) throws Exception{
		
//		File file = new File("C:/Users/jiashengxue/git/prunuus-example/prunuus-example-util/build/libs/libs/fastjson-1.2.8.jar");
//		
//		URL[] urls = new URL[]{
//				new URL("file:" + file.getAbsolutePath())
//		};
//		
//		ClassLoader classLoader = new URLClassLoader(urls, Thread.currentThread().getClass().getClassLoader());
//		
//		Thread.currentThread().setContextClassLoader(classLoader);
//		
//		Class.forName("com.alibaba.fastjson.JSON");
		
//		String dirBase = "C:/Users/jiashengxue/git/prunuus-example/prunuus-example-util/build/libs/prunuus-example-util-0.0.1-SNAPSHOT.jar";
//		
//		JarFile jarFile = new JarFile(new File(dirBase));
//		for(Enumeration<JarEntry> jarentries= jarFile.entries(); jarentries.hasMoreElements(); ){
//			JarEntry jarEntry = jarentries.nextElement();
//			System.out.println(jarEntry.getName());
////			System.out.println(jarEntry.getComment());
//		}
		
		
//		new FastjsonUtils().test();
//		Random rand = new Random();
//		Map map = new HashMap();
//		for(int i = 0; i< 10; i++){
//			int rint = rand.nextInt(1000);
//			map.put("k" + rint, "v" + rint);
//		}
		
//		System.out.println(JSON.toJSONString(map));

	}

}
